package org.jeecg.modules.demo.weixin.smtyuyuelist.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.jeecg.modules.demo.mall.api.MallHelloApi;
import org.jeecg.modules.demo.mall.entity.YxUser;
import org.jeecg.modules.demo.weixin.entity.SmtDev;
import org.jeecg.modules.demo.weixin.entity.SmtYuyueList;
import org.jeecg.modules.demo.weixin.merchantpay.service.IMerchantPayService;
import org.jeecg.modules.demo.weixin.smtdev.service.ISmtDevService;
import org.jeecg.modules.demo.weixin.smtmerchantshoprecord.service.ISmtMerchantShopRecordService;
import org.jeecg.modules.demo.weixin.smtoncecardusers.service.ISmtOnceCardUsersService;
import org.jeecg.modules.demo.weixin.smtyuyuelist.mapper.SmtYuyueListMapper;
import org.jeecg.modules.demo.weixin.smtyuyuelist.service.ISmtYuyueListService;
import org.jeecg.modules.demo.weixin.smtyuyueuser.mapper.SmtYuyueUserMapper;
import org.jeecg.modules.demo.weixin.smtyuyueuser.service.ISmtYuyueUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: smt_yuyue_list
 * @Author: jeecg-boot
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Service
public class SmtYuyueListServiceImpl extends ServiceImpl<SmtYuyueListMapper, SmtYuyueList> implements ISmtYuyueListService {

    @Autowired
    private SmtYuyueListMapper smtYuyueListMapper;

    @Autowired
    private MallHelloApi mallHelloApi;
    @Autowired
    private ISmtYuyueUserService yuyueUserService;
    @Resource
    private SmtYuyueUserMapper smtYuyueUserMapper;
    @Autowired
    private ISmtOnceCardUsersService smtOnceCardUsersService;
    @Autowired
    private ISmtMerchantShopRecordService smtMerchantShopRecordService;
    @Autowired
    private IMerchantPayService merchantPayService;
    @Autowired
    private ISmtDevService smtDevService;

    @SneakyThrows
    @Override
    public Page<SmtYuyueList> queryPageList(Page page, SmtYuyueList smtYuyueList) throws ParseException {
//        List<YxUser> yxUsers = mallHelloApi.queryUserList(smtYuyueList.getMerId());
        //根据门店查询设备

        LambdaQueryWrapper<SmtDev> smtDevLambdaQueryWrapper = new LambdaQueryWrapper<>();
        smtDevLambdaQueryWrapper.eq(SmtDev::getMerchantId, smtYuyueList.getMerId());
        List<SmtDev> smtDevList = smtDevService.list(smtDevLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(smtDevList)) {
            return new Page<>();
        }
        List<String> smtDevCodeList = smtDevList.stream().map(SmtDev::getDevCode).collect(Collectors.toList());

        Date dateBegin = null;
        Date dateEnd = null;
        if (StringUtils.hasText(smtYuyueList.getTimeStrat()) && StringUtils.hasText(smtYuyueList.getTimeStrat())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (smtYuyueList.getDetermine()!=null&&smtYuyueList.getDetermine()==1) {
                dateBegin=new Date();
            }else {
            dateBegin = sdf.parse(smtYuyueList.getTimeStrat());}
            dateEnd = sdf.parse(smtYuyueList.getTimeEnd());
        }
        Page<SmtYuyueList> smtYuyueListPage = new Page();
        LocalTime localTime = LocalTime.of(12, 00, 00);
        if (CollectionUtils.isEmpty(smtDevCodeList)) {
            smtYuyueListPage = smtYuyueListMapper.queryPageList(page, smtYuyueList, localTime, dateBegin, dateEnd, null);
        } else {
            smtYuyueListPage = smtYuyueListMapper.queryPageList(page, smtYuyueList, localTime, dateBegin, dateEnd, smtDevCodeList);
        }
        return smtYuyueListPage;
    }


    @Override
    public List<String> queryMerList(String merId) {
        //获取门店查询设备

        //获取门店查询设备
//        LambdaQueryWrapper<SmtDev> smtDevLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        smtDevLambdaQueryWrapper.eq(SmtDev::getMerchantId,merId);
//        List<SmtDev> smtDevList = smtDevService.list(smtDevLambdaQueryWrapper);
//        if (CollectionUtils.isEmpty(smtDevList)) {
//            return null;
//        }
//        //根据设备获取场次
//        List<String> devString = smtDevList.stream().map(SmtDev::getDevCode).collect(Collectors.toList());

        //获取门店用户
        List<YxUser> yxUsers = mallHelloApi.queryUserList(merId);
        if (CollectionUtils.isEmpty(yxUsers)) {
            return null;
        }
        //根据用户获取场次 id
        List<Integer> uids = yxUsers.stream().map(YxUser::getUid).collect(Collectors.toList());
        LambdaQueryWrapper<SmtYuyueList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SmtYuyueList::getUserId, uids);
        List<SmtYuyueList> smtYuyueLists = smtYuyueListMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(smtYuyueLists)) {
            return null;
        }
        List<String> yuyueIds = smtYuyueLists.stream().map(SmtYuyueList::getId).collect(Collectors.toList());

        return yuyueIds;
    }

    @Override
    public Page<SmtYuyueList> queryMList(Page page, SmtYuyueList smtYuyueList) throws ParseException {




        Date dateBegin = null;
        Date dateEnd = null;
        if (StringUtils.hasText(smtYuyueList.getTimeStrat()) && StringUtils.hasText(smtYuyueList.getTimeStrat())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateBegin = sdf.parse(smtYuyueList.getTimeStrat());
            dateEnd = sdf.parse(smtYuyueList.getTimeEnd());
        }
        Page<SmtYuyueList> smtYuyueListPage = new Page();
        LocalTime localTime = LocalTime.of(12, 00, 00);

        if (StringUtils.hasText(smtYuyueList.getMerId())) {
            LambdaQueryWrapper<SmtDev> smtDevLambdaQueryWrapper = new LambdaQueryWrapper<>();
            smtDevLambdaQueryWrapper.eq(SmtDev::getMerchantId, smtYuyueList.getMerId());
            List<SmtDev> smtDevList = smtDevService.list(smtDevLambdaQueryWrapper);
            if (CollectionUtils.isEmpty(smtDevList)) {
                return new Page<>();
            }
            List<String> smtDevCodeList = smtDevList.stream().map(SmtDev::getDevCode).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(smtDevCodeList)) {
                smtYuyueListPage = smtYuyueListMapper.queryMList(page, smtYuyueList, localTime, dateBegin, dateEnd, null);
            } else {
                smtYuyueListPage = smtYuyueListMapper.queryMList(page, smtYuyueList, localTime, dateBegin, dateEnd, smtDevCodeList);
            }
        }else smtYuyueListPage = smtYuyueListMapper.queryMList(page, smtYuyueList, localTime, dateBegin, dateEnd, null);
        return smtYuyueListPage;
    }


}
